﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.基础设置;
using B.S.DataBase.MyProject.Domain.诊疗管理;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using Dm.filter;
using MediatR;
<<<<<<<< HEAD:MedicalPCTerminal.Write.API/Appcation/CommandHandlers/DiagnosisTreatmentCommandHandler/AddCollectionManagementCommandHandler.cs
using MedicalPCTerminal.Write.API.Appcation.Commands.DiagnosisTreatmentCommand;
using Yitter.IdGenerator;

namespace MedicalPCTerminal.Write.API.Appcation.CommandHandlers.DiagnosisTreatmentCommandHandler
========
using MedicalPCTerminal.Write.API.Appcation.Commands.诊疗管理;
using Yitter.IdGenerator;

namespace MedicalPCTerminal.Write.API.Appcation.CommandHandlers.诊疗管理
>>>>>>>> parent of fe317f5 (修改命名空间):MedicalPCTerminal.Write.API/Appcation/CommandHandlers/诊疗管理/AddCollectionManagementCommandHandler.cs
{
    /// <summary>
    /// 采集管理命令处理
    /// </summary>
    public class AddCollectionManagementCommandHandler : IRequestHandler<AddCollectionManagementCommand, APIResult<int>>
    {
        /// <summary>
        /// sqlsugar
        /// </summary>
        private readonly SqlSugarContext sugar;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="sugar">sqlsugar</param>
        public AddCollectionManagementCommandHandler(SqlSugarContext sugar)
        {
            this.sugar = sugar;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public async Task<APIResult<int>> Handle(AddCollectionManagementCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            try
            {
                //开启事务
                sugar.db.Ado.BeginTran();
                CollectionManagement coll=new CollectionManagement();
                coll.CollCode=request.CollCode;
                coll.CollName = request.CollName;
                coll.Sex = request.Sex;
                coll.IdentityCard = request.IdentityCard;
                coll.Height = request.Height;
                coll.Weight = request.Weight;
                coll.BIM = request.BIM;
                coll.FastingBloodGlucose = request.FastingBloodGlucose;
                coll.PostprandialBloodGlucose = request.PostprandialBloodGlucose;
                coll.Cholesterol = request.Cholesterol;
                coll.Triglyceride = request.Triglyceride;
                coll.HDLCholesterol = request.HDLCholesterol;
                coll.LDLCholesterol = request.LDLCholesterol;
                coll.UserId = request.UserId;
                coll.CreateTime = request.CreateTime;
                coll.IsDeleted = false;
                var r = await sugar.db.Insertable(coll).ExecuteCommandAsync();
                if (r > 0)
                {
                    request.BloodPressureTest.ForEach(c =>
                    {
                        c.UserId = coll.UserId;
                        c.CreateTime = coll.CreateTime;
                        c.UserName = coll.CollName;
                    });
                    var res = await sugar.db.Insertable(request.BloodPressureTest).ExecuteCommandAsync();
                    result.Code = APIEnum.添加成功;
                    result.Msg = APIEnum.添加成功.ToString();
                    result.data = r;
                    // 提交事务
                    sugar.db.Ado.CommitTran();
                }
                else
                {
                    result.Code = APIEnum.添加失败;
                    result.Msg = APIEnum.添加失败.ToString();
                    result.data = r;
                    // 回滚事务
                    sugar.db.Ado.RollbackTran();
                }
            }
            catch (Exception ex)
            {
                // 发生异常时回滚事务
                sugar.db.Ado.RollbackTran();
                // 记录异常信息
                Console.WriteLine($"添加设备管理信息时发生异常: {ex.Message}");
                result.Code = APIEnum.添加失败;
                result.Msg = APIEnum.添加失败.ToString();
                result.data = 0;
            }
            return await Task.FromResult(result);
        }
    }
}
